<?php

namespace App\Http\Requests\Wechat\User;

use App\Http\Controllers\App\WechatUserFavorsController;
use App\Http\Requests\ApiRequest;
use App\Models\OpenWechat\ScanEvent;
use App\Models\OpenWechat\WechatUser;
use App\Rules\DateBetween;
use App\Services\Locale\Language;
use Illuminate\Database\Query\Builder;
use Illuminate\Foundation\Http\FormRequest;
use Illuminate\Validation\Rule;

/**
 * @property string|null scan_event_id
 * @property string name
 * @property string openid
 * @property string phone
 * @property string|null born_at
 * @property string|null age
 * @property int sex
 */
class WechatUserUpdateRequest extends ApiRequest
{
    /**
     * Determine if the user is authorized to make this request.
     *
     * @return bool
     */
    public function authorize()
    {
        return true;
    }

    /**
     * @test
     *
     * Get the validation rules that apply to the request.
     *
     * @return array
     */
    public function rules()
    {
        $rules = [
            'openid'        => 'required|string',
            'appid'         => 'nullable|string',
            'name'          => 'nullable|string|max:'.self::MAX_STRING,
            'born_at'       => [
                'nullable', 'date_format:"Y-m-d"',
                new DateBetween(now()->subYears(100), now())
            ],
            'age'           => ['required_without:born_at', 'int', 'between:1,100'],
            'sex'           => ['required', 'int', Rule::in([0, 1, 2])],
            'scan_event_id' => ['nullable', 'int', 'min:1'],
            'phone'         => ['nullable', 'string', 'max:'.self::MAX_STRING],
        ];

        $rules = $this->checkWebUserPhoneUnique($rules);

        return $rules;
    }

    /**
     * @return array
     */
//    public function messages()
//    {
//        return [
//            'openid.*'        => '微信用户的openid必须是字符串',
//            'appid.*'         => '公众号的appid必须是字符串',
//            'name.*'          => '微信用户的名字必须是字符串',
//            'phone.*'         => '微信用户的手机必须是合法的手机号或座机',
//            'born_at.*'       => '微信用户的出生日期必须是合法日期',
//            'sex.*'           => '微信用户的性别必须是0,1,2',
//            'scan_event_id.*' => '扫码的场景值必须是数值'
//        ];
//    }

    /**
     * 当存在 openid 以 web- 开头说明是 H5 登录，通过 openid 找到 customer
     * 检查 phone 在同一个 customer id 下不能重复
     *
     * @param $rules
     *
     * @return mixed
     */
    protected function checkWebUserPhoneUnique(array $rules): array
    {
        $userModel = new WechatUser;

        if ($userModel->isWebUser($this->openid)) {
            // must be exist
            $wechat_user = $userModel::findByOpenId($this->openid);

            $rules['phone'] []= Rule::unique($wechat_user->getTable())
                ->where($this->uniqueSameCustomer($wechat_user->customer->id))
                ->ignore($wechat_user->id);
        }

        return $rules;
    }

    /**
     * @param int $customer_id
     *
     * @return \Closure
     */
    private function uniqueSameCustomer(int $customer_id): \Closure
    {
        return function (Builder $query) use ($customer_id) {
            return $query->where('customer_id', $customer_id);
        };
    }

}
